package com.baidu.bas.core.view;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liyuepeng
 * @date 2018-10-09.
 */
@Data
@Accessors(chain = true)
public class EruptApiModel implements Serializable {

    private Status status;

    private PromptWay promptWay = PromptWay.DIALOG;

    private String message;

    private Object data;

    private int code = 200;

    private boolean errorIntercept = true;

    public EruptApiModel(Status status, String message, Object data, PromptWay promptWay) {
        this.status = status;
        this.message = message;
        this.data = data;
        this.promptWay = promptWay;
    }

    /**
     * 从状态码中重置code
     * @return
     */
    public EruptApiModel resetCodeFromStatus(){
        this.code = status.code;
        return this;
    }

    public EruptApiModel(Status status, String message, PromptWay promptWay) {
        this.status = status;
        this.message = message;
        this.promptWay = promptWay;
    }

    public static EruptApiModel successApi() {
        return new EruptApiModel(Status.SUCCESS, null, null, PromptWay.MESSAGE);
    }

    public static EruptApiModel successApiWithId(Object obj) {
        Map<String,Object> params = new HashMap<>();
        params.put("id",obj);
        return new EruptApiModel(Status.SUCCESS, null, params, PromptWay.MESSAGE);
    }

    public static EruptApiModel successApi(String message, Object data) {
        return new EruptApiModel(Status.SUCCESS, message, data, PromptWay.MESSAGE).resetCodeFromStatus();
    }

    public static EruptApiModel successApi(Object data) {
        return new EruptApiModel(Status.SUCCESS, null, data, PromptWay.MESSAGE).resetCodeFromStatus();
    }

    public static EruptApiModel errorApi(String message) {
        return new EruptApiModel(Status.ERROR, message, null, PromptWay.DIALOG).resetCodeFromStatus();
    }

    public static EruptApiModel errorApi(Status status, String message) {
        return new EruptApiModel(status, message, null, PromptWay.DIALOG).resetCodeFromStatus();
    }

    public static EruptApiModel errorApi(Status status) {
        return new EruptApiModel(status, "", null, PromptWay.MESSAGE).resetCodeFromStatus();
    }

    public static EruptApiModel errorNoInterceptApi(String message) {
        EruptApiModel eruptApiModel = new EruptApiModel(Status.ERROR, message, null, PromptWay.DIALOG);
        eruptApiModel.errorIntercept = false;
        return eruptApiModel.resetCodeFromStatus();
    }

    public static EruptApiModel errorNoInterceptMessage(String message) {
        EruptApiModel eruptApiModel = new EruptApiModel(Status.ERROR, message, null, PromptWay.MESSAGE);
        eruptApiModel.errorIntercept = false;
        return eruptApiModel.resetCodeFromStatus();
    }

    public static EruptApiModel errorApi(Exception e) {
        e.printStackTrace();
        return new EruptApiModel(Status.ERROR, e.getMessage(), null, PromptWay.DIALOG).resetCodeFromStatus();
    }

    public enum Status {
        /**
         *
         */
        SUCCESS(200), ERROR(500), INFO(201), WARNING(503), NOT_FOUND(404), FORBIDDEN(401);

        public final int code;

        Status(int code){
            this.code = code;
        }
    }

    public enum PromptWay {
        /**
         *
         */
        DIALOG, MESSAGE, NOTIFY
    }


}


